﻿@using Fierce.MvcExtend.Controls.Manage

<div title="@ViewBag.Work.Menu.Name" class="easyui-panel" data-options="fit:true,border:false,iconCls:'@ViewBag.Work.Menu.Icon'">
    <table id="udpgrid"></table>
    <div id="udptools">@Easyui.Toolbar(ViewBag.Work, true)</div>
</div>

<script type="text/javascript">
    $(function () {
        //表格初始化
        var gridInit = function () {
            $("#udpgrid").datagrid({
                url: "/basic/userdepartmentpost/grid",
                nowrap: false,
                border: false,
                fit: true,
                singleSelect: true,
                loadMsg: "正在处理...",
                idField: "ID",
                toolbar: "#udptools",
                columns: [[
                    {
                        field: "Title", title: "机构职能", width: 228, align: "center",
                        formatter: function (value, row, index) {
                            try {
                                return row.BasicDepartment.Name + " - " + row.BasicPost.Name;
                            }
                            catch (e) { return ""; }
                        }
                    }
                ]]
            });
        }();
        //添加
        var insert = function () {
            var row = $("#ugrid").datagrid("getSelected");
            if (row) {
                mutual("添加", null, row.ID);
            }
            else { manage.slide("请选择用户"); }
        };
        //修改
        var update = function () {
            var row = $("#udpgrid").datagrid("getSelected");
            if (row) {
                mutual("修改", row.ID, null);
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //删除
        var remove = function () {
            var row = $("#udpgrid").datagrid("getSelected");
            if (row) {
                manage.confirm("确认删除?", function (del) {
                    if (del) {
                        manage.ajax({
                            url: "/basic/userdepartmentpost/remove",
                            type: "post", data: { id: row.ID },
                            success: function (result) {
                                manage.slide(result.message);
                                if (result.success) { reload(); }
                            }
                        });
                    }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //实体操作
        var mutual = function (title, id, userid) {
            var href = "/basic/userdepartmentpost/page?" + $.param({ id: id, userid: userid });
            var d = manage.dialog({
                title: title, href: href, width: 880,
                handler: function () {
                    var deptrow = $("#udpdgrid").treegrid("getSelected");
                    if (deptrow) {
                        $("#departmentid").val(deptrow.ID);
                    }
                    else {
                        manage.msg("请选择组织机构"); return;
                    }
                    var postrow = $("#udppgrid").datagrid("getSelected");
                    if (postrow) {
                        $("#postid").val(postrow.ID);
                    }
                    else {
                        manage.msg("请选择赋予职能"); return;
                    }
                    manage.form({
                        dialog: d, formid: "#udpform",
                        url: "/basic/userdepartmentpost/save",
                        success: function (result) { reload(); }
                    });
                },
                onLoad: function () { formInit(id); }
            });
        };
        //弹窗初始化
        var formInit = function (id) {
            $("#udpdgrid").treegrid({
                url: "/basic/department/grid",
                nowrap: false, rownumbers: true,
                fit: true, singleSelect: true, border: false,
                idField: "ID", treeField: "Name", parentField: "Pid",
                columns: [[
                    { field: "Name", title: "名称", width: 222, align: "left" }
                ]],
                onLoadSuccess: function (row, data) {
                    if (id) { $("#udpdgrid").treegrid("select", $("#departmentid").val()); }
                }
            });
            $("#udppgrid").datagrid({
                url: "/basic/post/allgrid",
                nowrap: false, rownumbers: true, border: false,
                fit: true, singleSelect: true, idField: "ID",
                columns: [[
                    { field: "Name", title: "名称", width: 160, align: "center" },
                    { field: "Remark", title: "描述", width: 305, align: "center" }
                ]],
                onLoadSuccess: function (data) {
                    if (id) { $("#udppgrid").datagrid("selectRecord", $("#postid").val()); }
                }
            });
        };
        //刷新列表
        var reload = function () {
            $("#udpgrid").datagrid("unselectAll");
            $("#udpgrid").datagrid("reload");
        };
        //按钮事件
        manage.buttonInit("basicuserdepartmentpost", function (f) { return eval(f); });
    });
</script>